package {
    import org.flixel.*;
    
    public class Environment extends FlxGroup {
        private var _max_stars:int = 20;
        private var _recycler:FlxRecycler = new FlxRecycler();
        private var _timer:FlxTimer = new FlxTimer(1.5, getStar);

        public function Environment() {
            super();
            loadStars();
        }

        private function createStar():FlxSprite {
            var s:FlxSprite = new FlxSprite();
            s.loadGraphic(Resources.stars, true, false, 3, 5);
            s.addAnimation("sort", [FlxU.random()*3],0, false);
            s.play("sort");
            s.velocity.x = -40;
            return s;
        }

        public function getStar():void {
            StatePlay(FlxG.state).scene.add(
                _recycler.getObject(FlxG.width, FlxU.random() * FlxG.height));
        }

        private function loadStars():void {
            for(var i:int = 0;i < _max_stars;i++) {
                _recycler.add(createStar());
            }

            FlxG.addTimer(_timer).start();
        }

        override public function update():void {
            super.update();
        }

        override public function destroy():void {
            FlxG.removeTimer(_timer);
            super.destroy();
        }
    }
}
